package com.nuoyun.tnvmue.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;

import com.nuoyun.tnvmue.MyApp;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author JiangAdmin
 * date: 2020/4/10
 * Email: jiangmr@vip.qq.com
 * TODO: 工具
 */

public class ToolUtils {
    private static final String TAG = "ToolUtils";

    public static Context context() {
        return MyApp.getContext();
    }

    /**
     * 获取国家码
     */
    public static String getCountryZipCode() {
        String CountryZipCode = "";
        Locale locale = context().getResources().getConfiguration().locale;
        CountryZipCode = locale.getCountry();

        return CountryZipCode;
    }


    /**
     * Json 转成 Map<>
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, String> getMapForJson(String jsonStr) {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonStr);

            Iterator<String> keyIter = jsonObject.keys();
            String key;
            String value;
            Map<String, String> valueMap = new HashMap<>();
            while (keyIter.hasNext()) {
                key = keyIter.next();
                value = jsonObject.get(key).toString();
                valueMap.put(key, value);
            }
            return valueMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 除法
     *
     * @param v1
     * @param v2
     * @param scale 精度，到小数点后几位
     * @return
     */

    public static double div(String v1, String v2, int scale) {

        if (scale < 0) {

            throw new IllegalArgumentException("The scale must be a positive integer or ");

        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.divide(b2, scale, BigDecimal.ROUND_DOWN).doubleValue();

    }

    /**
     * Map 转 Json
     *
     * @param paramMap
     * @return
     */
    public static JSONObject map2Json(Map<String, Object> paramMap) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (paramMap != null && !paramMap.isEmpty()) {
                Set<Map.Entry<String, Object>> paramSet = paramMap.entrySet();
                for (Map.Entry<String, Object> entry : paramSet) {
                    String key = entry.getKey();
//                    if (key.equals("sign")) {
//                        continue;
//                    }
                    String value = String.valueOf(entry.getValue());
                    if (TextUtils.isEmpty(value)) {
                        continue;
                    }
                    jsonObject.put(key, value);
                }
            }
        } catch (JSONException e) {
            LogUtil.e(TAG, e.getMessage());
        }

        return jsonObject;
    }

    /**
     * map转String
     *
     * @param paramMap
     * @return
     */
    public static String map2String(Map<String, String> paramMap, String secretKey) {
        StringBuilder sb = new StringBuilder();
        sb.append(secretKey);
        if (paramMap != null && !paramMap.isEmpty()) {
            Set<Map.Entry<String, String>> paramSet = paramMap.entrySet();
            for (Map.Entry<String, String> entry : paramSet) {
                String key = entry.getKey();

                String value = entry.getValue();
                if (value == null) {
                    value = "";
                }
//                if (value != null && !"".equals(value)) {
                sb.append(key);
//                    sb.append(join);
                sb.append(value);
//                }
            }
        }
        LogUtil.e(TAG, "签名字符串拼装结果为:" + sb.toString());
        return sb.toString();
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean isCardID(String cardId) {
        if (TextUtils.isEmpty(cardId)) {
            return true;
        }
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * 验证是否包含 字母和数字  并验证8-16位
     *
     * @param str 传入的值
     * @return 符合为true
     */
    public static boolean isNormal(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        if (str.trim().length() >= 8 && str.trim().length() <= 16) {
            String regexNum = ".*[0-9]+.*";
            Pattern pattern = Pattern.compile(regexNum);
            Matcher isNum = pattern.matcher(str);
            String regexEn = ".*[a-zA-Z]+.*";
            Pattern patternEn = Pattern.compile(regexEn);
            Matcher isEnglish = patternEn.matcher(str);
            if (isNum.matches() && isEnglish.matches()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    /**
     * 验证身份证号
     *
     * @param str
     * @return
     */
    public static boolean IsIDcard(String str) {
        String regex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
        return match(regex, str);
    }

    /**
     * 数据正则对比
     *
     * @param regex 正则
     * @param str   数据
     * @return
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * @param dp 要转换的 dp
     * @return 转出 px
     */
    public static int dp2px(float dp) {
        // 拿到屏幕密度
        float density = MyApp.getContext().getResources().getDisplayMetrics().density;
        int px = (int) (dp * density + 0.5f);// 四舍五入
        return px;
    }


    /**
     * @param px 要转换的px
     * @return 转出 dp
     */
    public static float px2dp(int px) {
        float density = context().getResources().getDisplayMetrics().density;
        float dp = px * density;
        return dp;

    }

    /**
     * 版本名
     *
     * @return
     */
    public static String getVersionName() {
        return getPackageInfo().versionName;
    }

    /**
     * 版本号
     *
     * @return
     */
    public static int getVersionCode() {
        return getPackageInfo().versionCode;
    }

    private static PackageInfo getPackageInfo() {
        PackageInfo pi = null;

        try {
            PackageManager pm = context().getPackageManager();
            pi = pm.getPackageInfo(context().getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(TAG, e.getMessage());
        }

        return pi;
    }

    /**
     * 将String型格式化,比如想要将2011-11-11格式化成2011年11月11日,就StringPattern("2011-11-11","yyyy-MM-dd","yyyy年MM月dd日").
     *
     * @param date       String 想要格式化的日期
     * @param newPattern String 想要格式化成什么格式
     * @return String
     */
    public static String StringPattern(String date, String newPattern) {
        if (date == null || newPattern == null) {
            return "";
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(newPattern);        // 实例化模板对象
        Date d = null;
        try {
            d = sdf1.parse(date);   // 将给定的字符串中的日期提取出来
        } catch (Exception e) {            // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace();       // 打印异常信息
            LogUtil.e(TAG, e.getMessage());
        }
        return sdf2.format(d);
    }

    /**
     * 获得状态栏的高度
     *
     * @return
     */
    public static int getStatusHeight() {

        int statusHeight = -1;
        try {
            Class clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context().getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(TAG, e.getMessage());
        }
        return statusHeight;
    }

    /**
     * 判断 程序是否存在
     *
     * @param packageName
     * @return
     */
    public static boolean checkApplication(String packageName) {
        if (packageName == null || "".equals(packageName)) {
            return false;
        }
        try {
            context().getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 复制到系统粘贴板
     */
    public static void copyText(Context context, String string) {
        if (!TextUtils.isEmpty(string)) {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", string);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            Toast.makeText(context, "已复制", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "复制内容为空", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 保留两位小数
     *
     * @param v
     * @return
     */
    public static String float2(float v) {
        DecimalFormat fnum = new DecimalFormat("##0.00");
        return fnum.format(v);
    }

    /**
     * 验证文件是否存在
     *
     * @param strFile
     * @return
     */
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * 获得String中的数字
     *
     * @param s
     * @return
     */
    public static int StringInInt(String s) {

        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        return Integer.parseInt(m.replaceAll("").trim());

    }

    /**
     * 中文转 二进制
     *
     * @param s
     * @return
     */
    public static String stringToUnicode(String s) {
        try {
            StringBuffer out = new StringBuffer("");
            //直接获取字符串的unicode二进制
            byte[] bytes = s.getBytes("unicode");
            //然后将其byte转换成对应的16进制表示即可
            for (int i = 0; i < bytes.length - 1; i += 2) {
                out.append("\\u");
                String str = Integer.toHexString(bytes[i + 1] & 0xff);
                for (int j = str.length(); j < 2; j++) {
                    out.append("0");
                }
                String str1 = Integer.toHexString(bytes[i] & 0xff);
                out.append(str1);
                out.append(str);
            }
            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    public static int getDisplayWidth() {
        DisplayMetrics dm = context().getResources().getDisplayMetrics();
        return dm.widthPixels;

    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    public static int getDisplayHeight() {
        DisplayMetrics dm = context().getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    public static int getDisplayHeight(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            DisplayMetrics metrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(metrics);
            return metrics.heightPixels;
        }

        return 0;
    }


    /**
     * 手机号 去国家码
     *
     * @param country
     * @param phone
     * @return
     */
    public static String PhoneHandle(String country, String phone) {
        if (TextUtils.isEmpty(country) || TextUtils.isEmpty(phone)) {
            return "";
        }

        return phone.substring(country.length());
    }


    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public static void CallPhone(Context context, String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data;
        if (phoneNum.contains("#")) {
            data = Uri.parse("tel:" + Uri.encode(phoneNum));
        } else {
            data = Uri.parse("tel:" + phoneNum);
        }
        intent.setData(data);
        context.startActivity(intent);
    }

    /**
     * 获取蓝牙MAC地址
     *
     * @return
     */
    public static String getBtAddressByReflection() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Field field = null;
        try {
            field = BluetoothAdapter.class.getDeclaredField("mService");
            field.setAccessible(true);
            Object bluetoothManagerService = field.get(bluetoothAdapter);
            if (bluetoothManagerService == null) {
                return null;
            }
            Method method = bluetoothManagerService.getClass().getMethod("getAddress");
            if (method != null) {
                Object obj = method.invoke(bluetoothManagerService);
                if (obj != null) {
                    return obj.toString();
                }
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;

    }


}
